﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="VoteAdd.aspx.cs" Inherits="Meeting.Web.Admin.BaseSet.VoteAdd" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>投票添加</title>
    <script type="text/javascript" src="../../Scripts/jquery-1.8.2.min.js"></script>
    <link href="../../Css/main.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../Scripts/themes/icon.css" />
    <link rel="stylesheet" href="../../Css/zTreeStyle/zTreeStyle.css" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../Scripts/themes/cupertino/easyui.css" />
    <script type="text/javascript" src="../../Scripts/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../../Scripts/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="../../Scripts/common.js"></script>
    <script type="text/javascript" src="../../Scripts/jquery.ztree.core-3.5.min.js"></script>
    <script type="text/javascript" src="../../Scripts/jquery.ztree.excheck-3.5.js"></script>
      <script src="../../Scripts/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
    <!--ardialog-->
    <script type="text/javascript" src="../../Scripts/artDialog4.1.6/artDialog.js?skin=blue"></script>
    <script type="text/javascript" src="../../Scripts/artDialog4.1.6/plugins/iframeTools.js"></script>
    <script src="../../Scripts/Json2.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#trMin").hide();
            $("#trMax").hide();
            $("#trSubOffice").hide();
            $("#category").combobox(
              {
                  onSelect: function (rec) {
                      VoteTypeChange();
                  }
              });
            InitItemList();
        });
        //投票类型选择
        function VoteTypeChange() {
            var voteType = $("#category").combobox("getValue");
            if (voteType == 4) {
                $("#trMin").show();
                $("#trMax").show();
                $("#trSubOffice").show();
                //获取分局信息
                $('#officeList').datagrid({
                     
                    url: "VoteOperation.ashx?method=GetSubOfficeJosnForGrid",
                    onClickRow: function (index) {
                        $('#officeList').datagrid('beginEdit', index);
                    }
                });
            } else {
                $("#trMin").hide();
                $("#trMax").hide();
                $("#trSubOffice").hide();
            }
        }
        //初始化投票项目
        function InitItemList() {
            singleSelect: true,
            $('#itemList').datagrid({
                toolbar: [{
                    text: '添加投票项',
                    iconCls: 'icon-add',
                    handler: function () {
                        AddItem();
                    }
                }, '-', {
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        $.messager.confirm('警告', '删除项目，确定吗?', function (e) {
                            if (!e)
                                return;
                            DeleteItem();
                        })
                    }
                }],
                onClickRow: function (index) {
                    $('#itemList').datagrid('beginEdit', index);
                }
            });
        }
        //新增投票项目
        function AddItem() {
            //State=1代表新增记录 =2代表修改记录
            $('#itemList').datagrid('appendRow', { Sort: 100, Value: '' });
        }
        function DeleteItem() {
            var row = $('#itemList').datagrid('getSelected');
            if (!row) {
                $.messager.alert("提示", '请选择要删除的行！');
                return;
            }
            if (row.Id > 0)
                $('#deleteid').val($('#deleteid').val() + row.Id + ",");
            var index = $('#itemList').datagrid('getRowIndex', row);
            $('#itemList').datagrid('deleteRow', index);
        }

        //保存数据

        //保存数据
        function SaveData() {

            $('#itemList').datagrid('acceptChanges'); 
            var theme = $("#theme").val(); 
            if (theme == "") {
                $.messager.alert("提示", '投票主题不能为空！');
                return;
            }
            var ScoreMin = $("#ScoreMin").val();
            var ScoreMax = $("#ScoreMax").val();
            var category = $("#category").combobox("getValue");
            var start = $("#start").val(); //.datetimebox('getValue');
            var end = $("#end").val(); //.datetimebox('getValue');

            //获取投票选项
            var rows = $('#itemList').datagrid('getRows');
            if (rows.length == 0) {
                $.messager.alert("提示", '请增加投票选项！');
                return;
            } 

            var items = "[";
            for (var i = 0; i < rows.length; i++) {
                items += "{";
                items += "Id:0,";
                items += "VoteId:0,";
                items += "Sort:\"" + rows[i].Sort + "\",";
                items += "Value:\"" + rows[i].Value + "\"";
                items += "},";
            }
            if (items.length > 0) items = items.substr(0, items.length - 1);
            items += "]";
              
            //分局投票
            var selOffice = "";
           
            if (category == 4) {
                rows = $('#officeList').datagrid('getSelections');
                if (rows.length == 0) {
                    $.messager.alert("提示", '请选择分局！');
                    return;
                }
                //
                for (i = 0; i < rows.length; i++) {
                    var ed = $('#officeList').datagrid('getEditor', { index: i, field: 'Sort' });
                    if (ed != null && $(ed.target).val() == "") $(ed.target).val("100");
                    $('#officeList').datagrid('endEdit', i);
                }
                selOffice = "[";
                for (var i = 0; i < rows.length; i++) {
                    selOffice += "{";
                    selOffice += "Id:0,";
                    selOffice += "SubOfficeId:" + rows[i].Id + ",";
                    selOffice += "Sort:" + rows[i].Sort + ",";
                    selOffice += "VoteId:0";
                    selOffice += "},";
                }
                if (selOffice.length > 0) selOffice = selOffice.substr(0, selOffice.length - 1);
                selOffice += "]";
            } 
            var postData = { id: 0, theme: theme, category: category, scoreMin: ScoreMin,scoreMax:ScoreMax, start: start, end: end, items: items,   selOffice: selOffice };
            //提交
            $.post("VoteOperation.ashx?method=save", postData, function (data) {
                if (data.success == true) {
                    $.messager.alert("提示", '保存成功');
                    //                    $("#mainList").datagrid('reload');
                    //                    $("#edit").dialog("close");
                    artDialog.close();
                } else {
                    $.messager.alert("提示", data.Message);
                }
            });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="showView1" cellpadding="1" cellspacing="1" align="center">
            <tr>
                <td class="title">
                    <span style="color: Red;">*</span> 投票主题:
                </td>
                <td class="td_Modify1">
                    <input id="theme" name="theme" type="text" class="easyui-validatebox" data-options="required:true"
                        style="width: 260px" />
                </td>
            </tr>
            <tr>
                <td class="title">
                    投票类型:
                </td>
                <td class="td_Modify1">
                    <select class="easyui-combobox" id="category" name="category" style="width: 260px;">
                        <option value="1" selected="selected">单选</option>
                        <option value="2">多选</option>
                        <option value="3">打分</option>
                        <option value="4">分局打分</option>
                    </select>
                </td>
            </tr>
            <tr id="trMin">
                <td class="title">
                    最小分值:
                </td>
                <td class="td_Modify1">
                    <input id="ScoreMin" name="ScoreMin" value="1" class="easyui-numberspinner" data-options="min:1,max:100"
                        style="width: 260px" />
                </td>
            </tr>
            <tr id="trMax">
                <td class="title">
                    最大分值:
                </td>
                <td class="td_Modify1">
                    <input id="ScoreMax" name="ScoreMax" value="10" class="easyui-numberspinner" data-options="min:1,max:100"
                        style="width: 260px" />
                </td>
            </tr>
            <tr>
                <td class="title">
                    发起时间:
                </td>
                <td class="td_Modify1">
                    <input id="start" onfocus="WdatePicker({startDate:'%y-%M-01 00:00',dateFmt:'yyyy-MM-dd HH:mm',alwaysUseStartDate:true})"
                        style="width: 260px" />
                </td>
            </tr>
            <tr>
                <td class="title">
                    截止时间:
                </td>
                <td class="td_Modify1">
                    <input id="end" onfocus="WdatePicker({startDate:'%y-%M-01 00:00',dateFmt:'yyyy-MM-dd HH:mm',alwaysUseStartDate:true})"
                        style="width: 260px" />
                </td>
            </tr>
            <tr>
                <td class="title">
                    投票项目:
                </td>
                <td class="td_Modify1">
                    <table id="itemList" style="width: 350px;">
                        <thead>
                            <th data-options="field:'Value',width:220,editor:'text'">
                                项目名称
                            </th>
                            <th data-options="field:'Sort',width:90,editor:'text'">
                                排序
                            </th>
                        </thead>
                    </table>
                </td>
            </tr>
            <tr id="trSubOffice">
                <td class="title">
                    分局选择:
                </td>
                <td class="td_Modify1"> 
                    <table id="officeList" class="easyui-datagrid" style="width: 350px;  ">
                        <thead>
                            <tr>
                                <th data-options="field:'ck',checkbox:true">
                                </th>
                                <th data-options="field:'Id',width:80 ,hidden:true">
                                </th>
                                <th data-options="field:'Value',width:80 ,hidden:true">
                                </th>
                                <th data-options="field:'Name',width:200">
                                    名称
                                </th>
                                <th data-options="field:'Sort',width:100,editor:'text'">
                                    排序
                                </th>
                            </tr>
                        </thead>
                    </table>
                </td>
            </tr>
            <tr id="trSubOffice">
                <td class="title">
                    &nbsp;</td>
                <td class="td_Modify1"> 
                    <a id="MeetingSave" class="easyui-linkbutton" onclick="SaveData();">保存</a></td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
